//
//  AIVipPlusController.m
//  Mia
//
//  Created by Vill on 2024/1/11.
//

#import "AIVipPlusController.h"
#import <WMZBanner/WMZBannerView.h>
#import "AIVipCollectionCell.h"
#import "AIVipMonView.h"         // 1 month
#import "AIVipYearView.h"        // 1 Year
#import "AIVipPremiumView.h"     // Premium
// 导入 SafariServices 框架
#import <SafariServices/SafariServices.h>

@interface AIVipPlusController ()<SFSafariViewControllerDelegate>

@property (nonatomic, strong) UIView *topView;      //顶部view
@property (nonatomic, strong) UILabel *vipTitle;    //标题文本
@property (nonatomic, strong) UIImageView *vipPlus; //标题logo
@property (nonatomic, strong) UIButton *closeBtn;   //关闭按钮
@property (nonatomic, strong) UIScrollView *containScrollerView; //容器
@property (nonatomic, strong) AIVipMonView *oneMonth;
@property (nonatomic, strong) AIVipYearView *oneYear;
@property (nonatomic, strong) AIVipPremiumView *premium; //vip 到期时间
@property (nonatomic, strong) UILabel *desLbl;   // 描述文本
@property (nonatomic, strong) UIButton *doneBtn; // 确认按钮

@property (nonatomic, strong) UIButton *policyBtn;
@property (nonatomic, strong) UIButton *restoreBtn;
@property (nonatomic, strong) UIButton *termsBtn;

@property (nonatomic, strong) UILabel *detailLbl;

@end

@implementation AIVipPlusController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(KNavigationBarHeight + PTW(40));
    }];
    [self.view addSubview:self.closeBtn];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(PTW(35));
        make.trailing.mas_equalTo(self.view).mas_offset(-PTW(12));
        make.top.mas_equalTo(self.view).mas_offset(KStatusBarHeight + PTW(5));
    }];
    [self.view addSubview:self.vipTitle];
    [self.vipTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view).mas_offset(-PTW(42));
        make.centerY.mas_equalTo(self.closeBtn);
    }];
    [self.view addSubview:self.vipPlus];
    [self.vipPlus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(PTW(84), PTW(28)));
        make.centerY.mas_equalTo(self.vipTitle);
        make.leading.mas_equalTo(self.vipTitle.mas_trailing);
    }];
    [self.view addSubview:self.containScrollerView];
    [self.containScrollerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(self.closeBtn.mas_bottom);
    }];
    
    WMZBannerParam *param =
    BannerParam()
   //自定义视图必传
   .wMyCellClassNameSet(@"AIVipCollectionCell")
   .wMyCellSet(^UICollectionViewCell *(NSIndexPath *indexPath, UICollectionView *collectionView, id model, UIImageView *bgImageView,NSArray*dataArr) {
              //自定义视图
       AIVipCollectionCell *cell = (AIVipCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([AIVipCollectionCell class]) forIndexPath:indexPath];
       [cell setImgView:model[@"icon"] withTitle:model[@"name"]];
       return cell;
   })
   .wFrameSet(CGRectMake(0, PTW(10), KScreenWidth, PTW(280)))
   .wDataSet([self getData])
   //自定义item的大小
   .wItemSizeSet(CGSizeMake(KScreenWidth, PTW(280)))
   //固定移动的距离
   .wContentOffsetXSet(1)
//   //自动滚动
   .wAutoScrollSet(YES)
    .wAutoScrollSecondSet(5)
    //循环
   .wRepeatSet(YES)
//   //整体左右间距  设置为size.width的一半 让最后一个可以居中
//   .wSectionInsetSet(UIEdgeInsetsMake(0,20, 0, BannerWitdh*0.55*0.3))
//   //间距
//   .wLineSpacingSet(20)
   .wBannerControlImageSet(@"ai_vip_normal")
   .wBannerControlSelectImageSet(@"ai_vip_select")
   .wBannerControlImageSizeSet(CGSizeMake(9, 9))
   .wBannerControlSelectImageSizeSet(CGSizeMake(24, 16))
   .wBannerControlSelectMarginSet(PTW(12))
    ;
    WMZBannerView *bannerView = [[WMZBannerView alloc]initConfigureWithModel:param];
    [self.containScrollerView addSubview:bannerView];
    [self.containScrollerView addSubview:self.oneMonth];
    [self.containScrollerView addSubview:self.oneYear];
    [self.containScrollerView addSubview:self.desLbl];
    
    CGFloat premiumHeight = PTW(10);
    AIUserInfoModel *model = [[AIUserInfoManager shareManager] userInfo];
    // 添加VIP到期时间
    if ([model.vipExpireTime strHasValue]){
        [self.containScrollerView addSubview:self.premium];
        premiumHeight = PTW(30);
    }
    
    self.doneBtn.frame = CGRectMake(PTW(20), PTW(560) + premiumHeight, KScreenWidth-PTW(40), PTW(55));
    [self.containScrollerView addSubview:self.doneBtn];
    
    self.policyBtn.frame = CGRectMake((KScreenWidth - PTW(120))/2, self.doneBtn.maxY + PTW(10), PTW(120), PTW(40));
    [self.containScrollerView addSubview:self.policyBtn];
    
    self.termsBtn.frame = CGRectMake(KScreenWidth - PTW(90), self.doneBtn.maxY + PTW(10), PTW(90), PTW(40));
    [self.containScrollerView addSubview:self.termsBtn];
    
    self.restoreBtn.frame = CGRectMake(PTW(10), self.doneBtn.maxY + PTW(10), PTW(80), PTW(40));
    [self.containScrollerView addSubview:self.restoreBtn];
    
    self.detailLbl.frame = CGRectMake(PTW(20), self.policyBtn.maxY + PTW(10), KScreenWidth - PTW(40), PTW(150));
    [self.containScrollerView addSubview:self.detailLbl];
    [self.detailLbl sizeToFit];
    
    self.containScrollerView.contentSize = CGSizeMake(KScreenWidth,self.detailLbl.maxY + PTW(50));
}


- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.view setGradientBackgroundWithColors:@[[UIColor colorHex:@"#F2CFC7"],[UIColor colorHex:@"#FDADBC"],[UIColor colorHex:@"#6131A3"]] locations:@[@0, @0.27,@1] startPoint:CGPointMake(0.1,-0.1) endPoint:CGPointMake(1, 1)];
    [self.topView setGradientBackgroundWithColors:@[[UIColor colorHex:@"#000000" alpha:0.2],[UIColor colorHex:@"#000000" alpha:0]] locations:@[@0,@1] startPoint:CGPointMake(0,0) endPoint:CGPointMake(0, 1)];
}

#pragma mark ----------- func ---------
-(void)clickOneMonth:(BOOL)isOneMonth{
    if (isOneMonth){
        self.oneMonth.bgView.layer.borderColor = [UIColor whiteColor].CGColor;
        self.oneYear.bgView.layer.borderColor = [UIColor clearColor].CGColor;
        [UIView animateWithDuration:0.3 animations:^{
            self.oneMonth.transform = CGAffineTransformMakeScale(1.02, 1.02);
            self.oneYear.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
        self.desLbl.text = @"When due, the fee will be renewed automatically at $19.99 per month and can be cancelled at any time";
    }else{
        self.oneYear.bgView.layer.borderColor = [UIColor whiteColor].CGColor;
        self.oneMonth.bgView.layer.borderColor = [UIColor clearColor].CGColor;
        [UIView animateWithDuration:0.3 animations:^{
            self.oneYear.transform = CGAffineTransformMakeScale(1.02, 1.02);
            self.oneMonth.transform = CGAffineTransformMakeScale(1.0, 1.0);
        }];
        self.desLbl.text = @"When due, the fee will be renewed automatically at $5.83 per month and can be cancelled at any time";
    }
    self.doneBtn.enabled = YES;
    self.doneBtn.backgroundColor = [UIColor colorHex:@"#FFFFFF"];
    
}

// 确认按钮
- (void)doneAction{
    
}

// 协议
-(void)policyAction{
    // 创建一个 SFSafariViewController 对象并指定要打开的网页链接
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:url];
    // 设置 SFSafariViewController 的代理（可选）
    safariVC.delegate = self;
    [AIUIHelperManager.currentVC presentViewController:safariVC animated:YES completion:nil];
}

// terms
-(void)termsAction{
    
}

-(void)restoreAction{
    
}

// 关闭页面
-(void)closeAction{
    [self dissMissAction];
    if (self.closeBlock){
        self.closeBlock();
    }
}

#pragma mark ----------- lazy ---------
- (UIView *)topView{
    if(!_topView){
        _topView = [UIView new];
    }
    return _topView;
}

- (UILabel *)vipTitle{
    if(!_vipTitle){
        _vipTitle = [UILabel new];
        _vipTitle.text = @"MiaMind";
        _vipTitle.textAlignment = NSTextAlignmentCenter;
        _vipTitle.font = [UIFont fontWithWeight:MTBold size:PTW(31)];
        _vipTitle.textColor = UIColor.whiteColor;
    }
    return _vipTitle;
}

-(UIImageView *)vipPlus{
    if(!_vipPlus){
        _vipPlus = [UIImageView new];
        _vipPlus.image = KImaged(@"ai_vip_plus");
    }
    return _vipPlus;
}

-(UIButton *)closeBtn{
    if(!_closeBtn){
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeBtn setImage:KImaged(@"ai_main_close") forState:UIControlStateNormal];
        [_closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeBtn;
}

- (UIScrollView *)containScrollerView{
    if(!_containScrollerView){
        _containScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, KNavigationBarHeight, KScreenWidth, KScreenHeight - KNavigationBarHeight)];
        _containScrollerView.backgroundColor = UIColor.clearColor;
        _containScrollerView.showsVerticalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _containScrollerView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _containScrollerView;
}

- (NSArray*)getData{
    return @[
      @{@"name":@"Unlimited free text chat",@"icon":@"ai_vip_one"},
      @{@"name":@"Unlock horoscopes to \ndivine your fortune",@"icon":@"ai_vip_two"},
      @{@"name":@"Smarter psychological counseling \nAI understands you better",@"icon":@"ai_vip_three"},
      @{@"name":@"Supports all 16 types of AI \n3D characters",@"icon":@"ai_vip_four"},
      @{@"name":@"Tarot card reading: enjoy \nhalf price discount",@"icon":@"ai_vip_five"}
      ];
}

- (AIVipMonView *)oneMonth{
    if(!_oneMonth){
        _oneMonth = [[AIVipMonView alloc] initWithFrame:CGRectMake(PTW(12.5), PTW(300), KScreenWidth - PTW(25), PTW(85))];
        // 点击单月的
        WeakSelf(self);
        [_oneMonth addTapGestureWithBlock:^(UIGestureRecognizer * _Nonnull gesture) {
            [weakself clickOneMonth:YES];
        }];
    }
    return _oneMonth;
}

- (AIVipYearView *)oneYear{
    if(!_oneYear){
        _oneYear = [[AIVipYearView alloc] initWithFrame:CGRectMake(PTW(12.5), PTW(395), KScreenWidth - PTW(25), PTW(97.5))];
        // 点击单月的
        WeakSelf(self);
        [_oneYear addTapGestureWithBlock:^(UIGestureRecognizer * _Nonnull gesture) {
            [weakself clickOneMonth:NO];
        }];
    }
    return _oneYear;
}

- (UILabel *)desLbl{
    if(!_desLbl){
        _desLbl = [[UILabel alloc] initWithFrame:CGRectMake(PTW(15), PTW(515), KScreenWidth - PTW(30), PTW(40))];
        _desLbl.textAlignment = NSTextAlignmentCenter;
        _desLbl.font = [UIFont fontWithWeight:Bold size:PTW(12)];
        _desLbl.numberOfLines = 0;
        _desLbl.textColor = [UIColor colorHex:@"#FFFFFF" alpha:0.5];
        _desLbl.text = @"When due, the fee will be renewed automatically at $19.99 per month and can be cancelled at any time";
    }
    return _desLbl;
}

// vip到期时间
-(AIVipPremiumView *)premium{
    if (!_premium){
        _premium = [[AIVipPremiumView alloc]initWithFrame:CGRectMake(0, PTW(560), KScreenWidth, PTW(20))];
    }
    return _premium;
}

- (UIButton *)doneBtn{
    if(!_doneBtn){
        _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneBtn setTitle:@"Subscribe" forState:UIControlStateNormal];
        [_doneBtn setTitleColor:[UIColor colorHex:@"#5C5C5C"] forState:UIControlStateNormal];
        _doneBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        _doneBtn.backgroundColor = [UIColor colorHex:@"#EFEFEF" alpha:0.5];
        _doneBtn.layer.cornerRadius = PTW(55)/2;
        _doneBtn.layer.shadowColor = [UIColor colorHex:@"#FFFFFF" alpha:0.68].CGColor;
        _doneBtn.layer.shadowOffset = CGSizeMake(0,0);
        _doneBtn.layer.shadowOpacity = 1;
        _doneBtn.layer.shadowRadius = 8.5;
        [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
        _doneBtn.enabled = NO;
    }
    return _doneBtn;
}

- (UIButton *)policyBtn{
    if (!_policyBtn){
        _policyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_policyBtn setTitle:@"Privacy policy" forState:UIControlStateNormal];
        _policyBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        [_policyBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_policyBtn addTarget:self action:@selector(policyAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _policyBtn;
}

- (UIButton *)termsBtn{
    if (!_termsBtn){
        _termsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_termsBtn setTitle:@"Terms" forState:UIControlStateNormal];
        _termsBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        [_termsBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_termsBtn addTarget:self action:@selector(termsAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _termsBtn;
}

- (UIButton *)restoreBtn{
    if (!_restoreBtn){
        _restoreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_restoreBtn setTitle:@"Restore" forState:UIControlStateNormal];
        _restoreBtn.titleLabel.font = [UIFont fontWithWeight:MTBold size:PTW(15)];
        [_restoreBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_restoreBtn addTarget:self action:@selector(restoreAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _restoreBtn;
}

- (UILabel *)detailLbl{
    if(!_detailLbl){
        _detailLbl = [UILabel new];
        _detailLbl.numberOfLines = 0;
        _detailLbl.font = [UIFont fontWithWeight:Bold size:PTW(12)];
        _detailLbl.textColor = UIColor.whiteColor;
//        _detailLbl.text = @"Subscription are automatically renewed and unsubscribingrequires automatic renewing at least least 24 hours prior to theend of the current subscription, otherwise a subscription will becharged. your account will be charged for renewal within 24 hours of the current subscription. If the change is successfully, it will besuccessfully renewed. You can manage your subscriptions at anytime in the app store account settings";
        // 创建 NSMutableAttributedString 对象
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Subscription are automatically renewed and unsubscribingrequires automatic renewing at least least 24 hours prior to theend of the current subscription, otherwise a subscription will becharged. your account will be charged for renewal within 24 hours of the current subscription. If the change is successfully, it will besuccessfully renewed. You can manage your subscriptions at anytime in the app store account settings"];
        // 设置行距为 10.0
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3.0];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
        // 将属性文本设置给 UILabel
        [_detailLbl setAttributedText:attributedString];
    }
    return _detailLbl;
}

@end
